type RegExpsType = 'mobile' | 'email' | 'password' | 'loginName' | 'idCard';

/**
 * 正则集合
 */
export const regExps: {
  [k in RegExpsType]: RegExp;
} = {
  // external: /^(https?:|mailto:|tel:)/,
  mobile: /^1[3-5,7-8][0-9]{9}$/,
  // url: /^(https?|ftp):\/\/([a-zA-Z0-9.-]+(:[a-zA-Z0-9.&%$-]+)*@)*((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])){3}|([a-zA-Z0-9-]+\.)*[a-zA-Z0-9-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{2}))(:[0-9]+)*(\/($|[a-zA-Z0-9.,?'\\+&%$#=~_-]+))*$/,
  email:
    /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@(([[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/,
  password: /^(?=.*\d)(?=.*[a-zA-Z])[a-zA-Z\d]{6,}$/,
  loginName: /^(?=.*\d)(?=.*[a-zA-Z])[a-zA-Z\d]{6,}$/,
  idCard: /^(\d{6})(\d{4})(\d{2})(\d{2})(\d{3})([0-9Xx])$/,
};

/**
 * 正则提示集合
 */
export const regMsg: {
  [k in RegExpsType]: string;
} = {
  // external: '外部链接格式不正确',
  mobile: '手机号码格式不正确',
  // url: '链接格式不正确',
  email: '邮箱格式不正确',
  password: '密码格式不正确',
  loginName: '登录名格式不正确',
  idCard: '身份证格式不正确',
};

/**
 * 获取正则集合和正则提示
 * @param {keyof typeof regExps} key
 * @returns {{pattern: RegExp, message: string}}
 */
export const getPattern = (key: RegExpsType) => {
  return {
    pattern: regExps[key],
    message: regMsg[key],
  };
};

/**
 * 正则判断
 * @param {RegExp} reg
 * @param {string} str
 * @returns {boolean}
 */
export function isValidator(reg: RegExp, str: string) {
  return reg.test(str);
}

/**
 * 判断是否是数组
 * @param {Array} arg
 * @returns {Boolean}
 */
export function isArray(arg: any[]) {
  if (typeof Array.isArray === 'undefined') {
    return Object.prototype.toString.call(arg) === '[object Array]';
  }
  return Array.isArray(arg);
}

/**
 * 名字脱敏
 * @param {string} e
 * @returns {string}
 */
export const hideName = (e: string) => {
  if (!e) return '';
  const arr = [...String(e)];
  arr[1] = '*';
  return arr.join('');
};

/**
 * 手机号脱敏
 * @param {string} e
 * @returns {string}
 */
export const hidePhone = (e: string) => {
  if (!e) return '';
  const str = String(e);
  const reg = /(\d{3})\d*(\d{4})/g;
  return str.replace(reg, '$1****$2');
};

/**
 * 身份证脱敏
 * @param {string} e
 * @returns {string}
 */
export const hideIdNumber = (e: string) => {
  if (!e) return '';
  const str = String(e);
  const reg = /(\d{6})\d*(\d{4})/g;
  return str.replace(reg, '$1********$2');
};
